﻿/* Copyright (c) 2010, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    partial class Assembler
    {
        public class LockedInt
        {
            int _count = 0;
            public int Count { get { lock (this) { return _count; } } set { lock (this) { _count = value; } } }
            static public implicit operator int(LockedInt rtc) { return rtc.Count; }
            static public implicit operator LockedInt(int val) { LockedInt ret = new LockedInt(); ret.Count = val; return ret; }
            static public LockedInt operator ++(LockedInt rtc) { lock (rtc) { rtc._count++; return rtc; } }
            static public LockedInt operator --(LockedInt rtc) { lock (rtc) { rtc._count--; return rtc; } }
            public int Increment { get { lock (this) { return _count++; } } }
            public override string ToString() { lock (this) { return _count.ToString(); } }
        }

#if MT
        internal class TTCRequest { public TypeToCompile ttc; public IOutputFile of; }
        internal class MTCRequest { public MethodToCompile mtc; public IOutputFile of; }
        internal class FTCRequest { public FieldToCompile ftc; public IOutputFile of; }
#endif

        LockedInt rtc = new LockedInt();        /* number of threads running */

        public void DoCompile(IOutputFile w) { DoCompile(w, null); }
        public void DoCompile(IOutputFile w, IDoCompileFeedback dcf)
        {
            while (Requestor.MoreToDo || (rtc.Count > 0))
            {
                while (Requestor.MoreMethods)
                {
                    MethodToCompile? mtc = null;

                    lock (Requestor.meth_lock)
                    {
                        if (Requestor.MoreMethods)
                            mtc = Requestor.GetNextMethod();
                    }

                    if (mtc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleMethodFeedback(mtc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleMethod).Start(new MTCRequest { mtc = mtc.Value, of = w });
#else
                        AssembleMethod(mtc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreTypes)
                {
                    TypeToCompile? ttc = null;

                    lock (Requestor.type_lock)
                    {
                        if (Requestor.MoreTypes)
                            ttc = Requestor.GetNextType();
                    }

                    if (ttc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleTypeFeedback(ttc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleType).Start(new TTCRequest { ttc = ttc.Value, of = w });
#else
                        AssembleType(ttc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreTypeInfos)
                {
                    TypeToCompile? ttc = null;

                    lock (Requestor.ti_lock)
                    {
                        if (Requestor.MoreTypeInfos)
                            ttc = Requestor.GetNextTypeInfo();
                    }

                    if (ttc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleTypeInfoFeedback(ttc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleTypeInfo).Start(new TTCRequest { ttc = ttc.Value, of = w });
#else
                        AssembleTypeInfo(ttc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreFieldInfos)
                {
                    FieldToCompile? ftc = null;

                    lock (Requestor.fi_lock)
                    {
                        if (Requestor.MoreFieldInfos)
                            ftc = Requestor.GetNextFieldInfo();
                    }

                    if (ftc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleFieldInfoFeedback(ftc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleFieldInfo).Start(new FTCRequest { ftc = ftc.Value, of = w });
#else
                        AssembleFieldInfo(ftc.Value, w);
#endif
                    }
                }

                while (Requestor.MoreMethodInfos)
                {
                    MethodToCompile? mtc = null;

                    lock (Requestor.mi_lock)
                    {
                        if (Requestor.MoreMethodInfos)
                            mtc = Requestor.GetNextMethodInfo();
                    }

                    if (mtc.HasValue)
                    {
                        if (dcf != null)
                            dcf.AssembleMethodInfoFeedback(mtc.Value);
#if MT
                        rtc++;
                        new System.Threading.Thread(AssembleMethodInfo).Start(new MTCRequest { mtc = mtc.Value, of = w });
#else
                        AssembleMethodInfo(mtc.Value, w);
#endif
                    }
                }
            }
        }

        public interface IDoCompileFeedback
        {
            void AssembleMethodFeedback(Assembler.MethodToCompile mtc);
            void AssembleTypeFeedback(Assembler.TypeToCompile ttc);
            void AssembleTypeInfoFeedback(Assembler.TypeToCompile ttc);
            void AssembleFieldInfoFeedback(Assembler.FieldToCompile ftc);
            void AssembleMethodInfoFeedback(Assembler.MethodToCompile mtc);
        }
    }
}
